
import 'dart:async';

import 'package:flutter/services.dart';

class PinChat {
  static const MethodChannel _channel = MethodChannel('com.feibi.chat/pin_chat');

  /// 配置IMKit SDK
  static Future<void> configure({required String clientKey, required String serverUrl}) async {
    await _channel.invokeMethod('configure', <String, String>{
      'clientKey': clientKey,
      'serverUrl': serverUrl,
    });
  }

  /// 连接IMKit
  static Future<void> connect({required String userId, String? accessToken}) async {
    await _channel.invokeMethod('connect', <String, dynamic>{
      'userId': userId,
      'accessToken': accessToken,
    });
  }

  /// 开始一对一聊天
  static Future<void> startChat({required String toUserId}) async {
    await _channel.invokeMethod('startChat', <String, String>{
      'toUserId': toUserId,
    });
  }

  /// 连接IMKit并开始一对一聊天
  ///
  /// [accessToken]在sandbox/development环境可为空
  /// [nickname]和[avatarUrl]其中之一不为空时，会更新用户资料
  static Future<void> connectAndStartChat({
    required String userId,
    required String toUserId,
    String? accessToken,
    String? nickname,
    String? avatarUrl,
    String? description,
  }) async {
    await _channel.invokeMethod('connectAndStartChat', <String, dynamic>{
      'userId': userId,
      'toUserId': toUserId,
      'accessToken': accessToken,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'description': description,
    });
  }
  
  /// 更新当前用户信息
  static Future<void> updateMyProfile({
    String? nickname,
    String? avatarUrl,
    String? description,
  }) async {
    await _channel.invokeMethod('updateMyProfile', <String, String?>{
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'description': description,
    });
  }

  /// 进入聊天室列表(已经连接了IMKit)
  static Future<void> goChatRoomList() async {
    await _channel.invokeMethod('goChatRoomList');
  }
}
